home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 742 / rkrm_lib2 / rkrm_lib2.lha / Exec_Library / Lists / buildlist.c < prev   
C/C++ Source or Header  |  1992-09-03  |  5KB  |  152 lines

  1. ;/* buildlist.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 buildlist.c
  3. Blink FROM LIB:c.o,buildlist.o TO buildlist LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit ;
  5.  
  6. buildlist.c - example which uses an application-specific Exec list
  7.  
  8.  
  9. Copyright (c) 1992 Commodore-Amiga, Inc.
  10.  
  11. This example is provided in electronic form by Commodore-Amiga, Inc. for
  12. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  13. published by Addison-Wesley (ISBN 0-201-56774-1).
  14.  
  15. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  16. information on the correct usage of the techniques and operating system
  17. functions presented in these examples.  The source and executable code
  18. of these examples may only be distributed in free electronic form, via
  19. bulletin board or as part of a fully non-commercial and freely
  20. redistributable diskette.  Both the source and executable code (including
  21. comments) must be included, without modification, in any copy.  This
  22. example may not be published in printed form or distributed with any
  23. commercial product.  However, the programming techniques and support
  24. routines set forth in these examples may be used in the development
  25. of original executable software products for Commodore Amiga computers.
  26.  
  27. All other rights reserved.
  28.  
  29. This example is provided "as-is" and is subject to change; no
  30. warranties are made.  All use is at your own risk. No liability or
  31. responsibility is assumed.
  32. */
  33.  
  34. #include <exec/types.h>
  35. #include <exec/lists.h>
  36. #include <exec/nodes.h>
  37. #include <exec/memory.h>
  38.  
  39. #include <clib/alib_protos.h>
  40. #include <clib/exec_protos.h>
  41.  
  42. #include <string.h>
  43. #include <stdio.h>
  44.  
  45. #ifdef LATTICE
  46. int CXBRK(void)  { return(0); }  /* Disable Lattice CTRL/C handling */
  47. void chkabort(void) { return; }  /* really */
  48. #endif
  49.  
  50. /* Our function prototypes */
  51. VOID AddName(struct List *, UBYTE *);
  52. VOID FreeNameNodes(struct List *);
  53. VOID DisplayNameList(struct List *);
  54. VOID DisplayName(struct List *, UBYTE *);
  55.  
  56. struct NameNode {
  57.     struct Node nn_Node;        /* System Node structure */
  58.     UBYTE  nn_Data[62];         /* Node-specific data */
  59. };
  60.  
  61. #define NAMENODE_ID   100       /* The type of "NameNode" */
  62.  
  63. VOID main(VOID)
  64. {
  65.     struct  List    *NameList;    /* Note that a MinList would also work */
  66.  
  67.     if (!( NameList = AllocMem(sizeof(struct List),MEMF_CLEAR)) )
  68.         printf("Out of memory\n");
  69.     else {
  70.         NewList(NameList);        /* Important: prepare header for use */
  71.  
  72.         AddName(NameList,"Name7");   AddName(NameList,"Name6");
  73.         AddName(NameList,"Name5");   AddName(NameList,"Name4");
  74.         AddName(NameList,"Name2");   AddName(NameList,"Name0");
  75.  
  76.         AddName(NameList,"Name7");   AddName(NameList,"Name5");
  77.         AddName(NameList,"Name3");   AddName(NameList,"Name1");
  78.  
  79.         DisplayName(NameList,"Name5");
  80.         DisplayNameList(NameList);
  81.  
  82.         FreeNameNodes(NameList);
  83.         FreeMem(NameList,sizeof(struct List));  /* Free list header */
  84.     }
  85. }
  86.  
  87. /* Allocate a NameNode structure, copy the given name into the structure,
  88.  * then add it the specified list.  This example does not provide an
  89.  * error return for the out of memory condition.
  90. */
  91. VOID AddName(struct List *list, UBYTE *name)
  92. {
  93.     struct NameNode *namenode;
  94.  
  95.     if (!( namenode = AllocMem(sizeof(struct NameNode),MEMF_CLEAR) ))
  96.         printf("Out of memory\n");
  97.     else {
  98.         strcpy(namenode->nn_Data,name);
  99.         namenode->nn_Node.ln_Name = namenode->nn_Data;
  100.         namenode->nn_Node.ln_Type = NAMENODE_ID;
  101.         namenode->nn_Node.ln_Pri  = 0;
  102.         AddHead((struct List *)list,(struct Node *)namenode);
  103.     }
  104. }
  105.  
  106. /*
  107.  * Free the entire list, including the header.  The header is not updated
  108.  * as the list is freed.  This function demonstrates how to avoid
  109.  * referencing freed memory when deallocating nodes.
  110.  */
  111. VOID FreeNameNodes(struct List *list)
  112. {
  113.     struct NameNode *worknode;
  114.     struct NameNode *nextnode;
  115.  
  116.     worknode = (struct NameNode *)(list->lh_Head); /* First node */
  117.     while (nextnode = (struct NameNode *)(worknode->nn_Node.ln_Succ)) {
  118.         FreeMem(worknode,sizeof(struct NameNode));
  119.         worknode = nextnode;
  120.     }
  121.  
  122. }
  123.  
  124. /*
  125.  * Print the names of each node in a list.
  126.  */
  127. VOID DisplayNameList(struct List *list)
  128. {
  129. struct Node *node;
  130.  
  131.     if (list->lh_TailPred == (struct Node *)list)
  132.         printf("List is empty.\n");
  133.     else {
  134.         for (node = list->lh_Head ; node->ln_Succ ; node = node->ln_Succ)
  135.             printf("%lx -> %s\n",node,node->ln_Name);
  136.     }
  137. }
  138.  
  139. /*
  140.  * Print the location of all nodes with a specified name.
  141.  */
  142. VOID DisplayName(struct List *list, UBYTE *name)
  143. {
  144. struct Node *node;
  145.  
  146.     if (node = FindName(list,name)) {
  147.         while (node) {
  148.             printf("Found a %s at location %lx\n",node->ln_Name,node);
  149.             node = FindName((struct List *)node,name);
  150.         }
  151.     } else printf("No node with name %s found.\n",name);
  152. }